package component.controls
{
	import component.controls.interfaces.ISelected;
	import component.controls.ui.Component;
	import component.global.ComponentSetting;
	import component.global.manager.EventMgr;
	import component.global.manager.StageManager;
	import component.global.utils.DisplayUtils;
	import component.global.utils.GraphicsUtils;
	import component.global.utils.ObjectUtil;
	
	import flash.display.Shape;
	import flash.events.MouseEvent;
	
	public class CheckBox extends Component implements ISelected
	{
		public function CheckBox(label:String=ComponentSetting.LABEL_DEFAULT_STRING, selected:Boolean=false)
		{
			_labelStr = label;
			_selected = selected;
			super();
		}
		
		protected var _unselectedShape:Shape;
		protected var _selectedShape:Shape;
		protected var _labelStr:String;
		protected var _selected:Boolean;
		protected var _label:Label;
		protected var _isDownInside:Boolean;
		
		override protected function init():void{
			super.init();
			buttonMode = true;
			_unselectedShape = GraphicsUtils.createRectangle(ComponentSetting.CHECK_BOX_DEFAULT_SIZE,ComponentSetting.CHECK_BOX_DEFAULT_SIZE);
			_selectedShape = GraphicsUtils.createCheckBoxSelected();
			_label = new Label(_labelStr);
			_label.x = _unselectedShape.width + 4;
			_background = GraphicsUtils.createRectangle(_label.x + _label.width,_label.height);
			_background.filters = _selectedShape.filters = _unselectedShape.filters = _filters[0];
			_background.alpha = 0;
			if(selected){
				DisplayUtils.addDisplayObject(this, _background, _selectedShape, _label);
			} else {
				DisplayUtils.addDisplayObject(this, _background, _unselectedShape, _label);
			}
			
			EventMgr.Instance.addEvent(this,[MouseEvent.ROLL_OVER,MouseEvent.ROLL_OUT,MouseEvent.MOUSE_DOWN,MouseEvent.MOUSE_UP],[__onMouseEventHandler,__onMouseEventHandler,__onMouseEventHandler, __onMouseEventHandler]);
		}
		
		override protected function __onMouseEventHandler(e:MouseEvent):void{
			switch(e.type){
				case MouseEvent.ROLL_OVER:
					_selectedShape.filters = _unselectedShape.filters = _filters[1];
					break;
				case MouseEvent.MOUSE_UP:
					if(_isDownInside){
						selected = !selected;
						_isDownInside = false;
					}
					_selectedShape.filters = _unselectedShape.filters = _filters[1];
					break;
				case MouseEvent.ROLL_OUT:
					_selectedShape.filters = _unselectedShape.filters = _filters[0];
					break;
				case MouseEvent.MOUSE_DOWN:
					_isDownInside = true;
					EventMgr.Instance.addEvent(StageManager.Instance.stage,[MouseEvent.MOUSE_UP],[__onStageUp]);
					_selectedShape.filters = _unselectedShape.filters = _filters[2];
					break;
			}
		}
		
		private function __onStageUp(e:MouseEvent):void{
			_isDownInside = false;
			EventMgr.Instance.removeEvent(StageManager.Instance.stage,[MouseEvent.MOUSE_UP],[__onStageUp]);
		}
		
		public function get selected():Boolean{
			return _selected;
		}
		
		public function set selected(value:Boolean):void{
			if(_selected == value)return;
			_selected = value;
			if(_selected){
				DisplayUtils.removeDisplay(_unselectedShape);
				DisplayUtils.addDisplayObject(this, _selectedShape);
			} else {
				DisplayUtils.removeDisplay(_selectedShape);
				DisplayUtils.addDisplayObject(this,_unselectedShape);
			}
		}
		
		override public function dispose():void{
			super.dispose();
			EventMgr.Instance.removeEvent(this);
			EventMgr.Instance.removeEvent(StageManager.Instance.stage,[MouseEvent.MOUSE_UP],[__onStageUp]);
			ObjectUtil.dispose(_unselectedShape);_unselectedShape=null;
			ObjectUtil.dispose(_selectedShape);_selectedShape=null;
			ObjectUtil.dispose(_label);_label=null;
		}
	}
}